package com.hunan.api.catv.action;


import com.hunan.api.catv.common.CacheKey;
import com.hunan.api.catv.common.Constants;
import com.hunan.api.catv.entity.Result;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.enums.ResultCode;
import com.hunan.api.catv.entity.po.SystemUser;
import com.hunan.api.catv.entity.vo.SystemUserVo;
import com.hunan.api.catv.utils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;

public class BaseAction {

    public static Logger log = LoggerFactory.getLogger(BaseAction.class);

    @Autowired
    RedisUtils redisUtils;

    public String getRemoteIp(HttpServletRequest request) {
        if (!StringUtils.isEmpty(request.getParameter("clientIP"))) {
            return request.getParameter("clientIP");
        }
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip.split(",")[0];
    }

    public Integer getMemberId(HttpServletRequest request) {
        Object memberId = request.getAttribute("memberId");
        if (!ObjectUtils.isEmpty(memberId)) {
            return Integer.valueOf(memberId.toString());
        }
        return null;
    }

    public Integer getMemberIdV2(HttpServletRequest request) {
        Object memberId = request.getAttribute("memberId");
        String token = request.getHeader("token");
        if (!ObjectUtils.isEmpty(memberId)) {
            return Integer.valueOf(memberId.toString());
        } else if (!ObjectUtils.isEmpty(token)) {
            String memberKey = MessageFormat.format(CacheKey.memberToken, token);
            return (Integer) redisUtils.get(memberKey);
        }
        return null;
    }

    public String getMemberToken(HttpServletRequest request) {

        return request.getHeader("token");
    }

    public String getRefreshToken(HttpServletRequest request) {

        return request.getHeader("refreshToken");
    }

    public Integer getClientVersion(HttpServletRequest request) {

        return Integer.valueOf(request.getHeader("v"));
    }

    public Integer getChannel(HttpServletRequest request) {

        return Integer.valueOf(request.getHeader("c"));
    }

    public SystemUser getSystemUser(HttpServletRequest request) {
        Object sysUserVo = request.getAttribute("sysUser");
        if (!ObjectUtils.isEmpty(sysUserVo)) {
            SystemUserVo sv = (SystemUserVo) sysUserVo;
            SystemUser sysUser = new SystemUser();
            BeanUtils.copyProperties(sv, sysUser);
            return sysUser;
        }
        return null;
    }

    public Result build(ResultCode code, Object data, String msg) {
        Result result = new Result();
        result.setC(code.getCode());
        result.setD(data == null ? "" : data);
        result.setM(msg);
        return result;
    }

    public Result buildResult(ServiceResult serviceResult) {
        Result result = new Result();
        result.setD(serviceResult.getData());
        result.setC(serviceResult.getStatus() ? ResultCode.ok.getCode() : ResultCode.fail.getCode());
        result.setM(serviceResult.getMsg());
        return result;
    }

    public Result buildResultCode(ServiceResult serviceResult) {
        Result result = new Result();
        result.setD(serviceResult.getData());
        result.setC(serviceResult.getStatus() ? ResultCode.ok.getCode() : ResultCode.failResult.getCode());
        result.setM(serviceResult.getMsg());
        return result;
    }

    public Result buildOK(Object data, String msg) {
        Result result = new Result();
        result.setC(ResultCode.ok.getCode());
        result.setD(data == null ? "" : data);
        result.setM(StringUtils.isEmpty(msg) ? ResultCode.ok.getMsg() : msg);
        return result;
    }

    public Result buildFail(Object data, String msg) {
        Result result = new Result();
        result.setC(ResultCode.fail.getCode());
        result.setD(data);
        result.setM(StringUtils.isEmpty(msg) ? ResultCode.fail.getMsg() : msg);
        return result;
    }

    public Result buildRequire(String msg) {
        Result result = new Result();
        result.setC(ResultCode.require.getCode());
        result.setD("");
        result.setM(StringUtils.isEmpty(msg) ? ResultCode.fail.getMsg() : msg);
        return result;
    }

    public Result buildUnauthorized() {
        Result result = new Result();
        result.setC(ResultCode.unauthorized.getCode());
        result.setD("");
        result.setM(ResultCode.unauthorized.getMsg());
        return result;
    }

    public Result buildServiceResult(ServiceResult serviceResult) {
        if (serviceResult.getStatus()) {
            return buildOK(serviceResult.getData(), serviceResult.getMsg());
        }
        return buildFail(serviceResult.getData(), serviceResult.getMsg());
    }

    public Integer getPageSize(HttpServletRequest request) {
        String pageSize = request.getParameter("pageSize");
        if (!ObjectUtils.isEmpty(pageSize)) {
            Integer size = Integer.valueOf(pageSize);
            if (size > Constants.pageMax) {
                return Constants.pageMax;
            }
            return size;
        }
        return 20;
    }

    public String getDeviceId(HttpServletRequest request) {
        return request.getHeader("deviceId");
    }
}